package com.traffic.between.adaptor;

import java.util.Date;

import org.apache.log4j.Logger;

import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.BetweenProperties;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.ShuoRuanServiceImpl;
import com.traffic.managent.service.order.bean.OrderBean;

import net.sf.json.JSONObject;

@ChooseProvider(channel = "shuor")
public class ShuoRuanAdaptor implements IAdaptor{

	private static Logger log = Logger.getLogger(ShuoRuanAdaptor.class);

	public void order() {
		OrderBean orderBean = OrderInfoContext.getContext();
		orderBean.setSendTime(new Date());
		
		String productId = orderBean.getProductCode();
		String phoneNo = orderBean.getPhoneNo();
		String cstmOrderNo = orderBean.getIds();
		String productPrice = orderBean.getProviderPrice().toString();
		String vasPid = orderBean.getVasProdId();
	    
		String callBackUrl = BetweenProperties.CALLBACK_URL+"Shuoruan";

		String result = ShuoRuanServiceImpl.service.order(productId, phoneNo, cstmOrderNo, productPrice,vasPid,callBackUrl);
		log.info("ShuoRuanAdaptor order() result"+result);
		JSONObject json = JSONObject.fromObject(result);

		String code = String.valueOf(json.get("resultCode"));
	
		if (("00".equals(code))) {
		
			orderBean.setMessage((String) json.get("errorDesc"));
			orderBean.setErrorMsg("订购中");
			orderBean.setStatus("11");
		} else {
			orderBean.setMessage((String) json.get("errorDesc"));
			orderBean.setErrorMsg("订购失败");
			orderBean.setStatus("8");
		}
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}

	
	public void status() {

		/**
		 * 0：订单已受理，1：订购中，7：订购成功，8：订购失败 { "code": "0000", "msg": "成功", "data": {
		 * "orderNo": "GDP14656954660051458", "cstmOrderNo": "test1234567"
		 * "status": "8", "errorDesc": "该用户是黑名单用户" } }
		 * 
		 */
		OrderBean orderBean = OrderInfoContext.getContext();
		
		String cstmOrderNo = orderBean.getIds();
		String vasPid = orderBean.getVasProdId();
	
		String result = ShuoRuanServiceImpl.service.orderStatus(cstmOrderNo,vasPid);

		JSONObject json = JSONObject.fromObject(result);

		String code =String.valueOf(json.get("resultCode"));

		if ("01".equals(code)) {
			
			orderBean.setMessage((String) json.get("errorDesc"));
			orderBean.setErrorMsg("充值失败");
			orderBean.setStatus("8");
			
		}else if("00".equals(code)){
		
			orderBean.setMessage((String) json.get("errorDesc"));
			orderBean.setErrorMsg("充值成功");
			orderBean.setStatus("7");
		}else{
			
			orderBean.setMessage((String) json.get("errorDesc"));
			orderBean.setErrorMsg("订购中");
			orderBean.setStatus("1");
		}
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}
}
